package com.hzhjhh.view.manageView.menus.accountMng.register;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.UserInfo;
import com.hzhjhh.mapper.UserInfoMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.accountMng.RegisterDialog;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-仓库-账户管理-添加/修改用户-管理员登录弹出框-注册用户弹出框-注册
 * 
 * @author 12083
 *
 */
public class RegisterBt {

	private static UserInfoMapper userInfoMapper;
	
	/**
	 * 按钮
	 */
	public static JButton btInit(RegisterDialog registerDialog) {
		JButton cancel = new JButton();
		cancel.setBounds(75, 318, 83, 25);
		cancel.setText("注册");

		cancel.addActionListener(event -> {
			String cardId = registerDialog.getTxfCardId().getText();
			String cardOwner = registerDialog.getTxfCardOwner().getText();
			String IDCard = registerDialog.getTxfIDCard().getText();
//			String cardId = jTextField1.getText(); //卡号
//            String cardOwne = registerDialog.getTxfCardOwner().getText(); //持卡人
            String IDcard = registerDialog.getTxfIDCard().getText(); //身份证
            String password = registerDialog.getPasswordField().getText(); //密码
            String CardDate = registerDialog.getTxfCardOpenDate().getText(); //开卡日期
            String UserGrade = registerDialog.getCobUserType().getSelectedItem().toString(); //用户类型
            String term = registerDialog.getTxfTerm().getText(); //期限
            String score = registerDialog.getTxfScore().getText(); //积分
            String discount = registerDialog.getTxfDiscount().getText(); //折扣
            String remark = registerDialog.getTxaRemark().getText();
            
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName(cardOwner);
            userInfo.setUserPassword(password);
            userInfo.setUserType(UserGrade);
            userInfo.setCardId(Integer.parseInt(cardId));
            userInfo.setDiscount(Integer.parseInt(discount));
            userInfo.setUserId(IDcard);
            userInfo.setCardDate(DateUtil.parse(CardDate, DateFormatContants.YYYY_MM_DD_HH_MM_SS));
            userInfo.setTerm(term);
            userInfo.setScore(Integer.parseInt(score));
            userInfo.setRemark(remark);
            
            int row = userInfoMapper.insert(userInfo);
//    		int i = Insert_stock.Insert_User(cardId, cardOwne, IDcard, password,
//                    CardDate,
//                    UserGrade, term, score, discount,
//                    remark);
            if (row != 0) {
                JOptionPane.showMessageDialog(registerDialog, "开卡成功", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(registerDialog, "开卡失败", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }
		});
		
		// 初始化mapper
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
		
		return cancel;
	}
}
